import rclpy
from rclpy.node import Node
import os
import threading
from std_msgs.msg import Int32


class Keyboard(Node):
    def __init__(self):
        super().__init__('image_subscriber')
        self.get_logger().info("键盘监听")
        
        self.lock = threading.Lock()  
 
        self.key_publisher = self.create_publisher(Int32, '/sign4return', 1)  #
        self.vlm_publisher = self.create_publisher(Int32, '/vlm', 1)


def handle_input(keyboard):
    while True:
        command = input("按键p开始, 按键j启动大模型: ")
        if command == 'p':
            key = Int32()
            key.data = -1
            keyboard.key_publisher.publish(key)
        if command == "j":
            key = Int32()
            key.data = 1
            keyboard.vlm_publisher.publish(key)


def main(args=None):
    rclpy.init(args=args)
    keyboard = Keyboard()

    #input_thread = threading.Thread(target=handle_input, args=(keyboard), daemon=True)  #一个按键
    input_thread = threading.Thread(target=handle_input, args=(keyboard,), daemon=True)  #多个按键

    input_thread.start()

    rclpy.spin(keyboard)

    input_thread.join()

    keyboard.destroy_node()

if __name__ == '__main__':
    main()
